﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using INF1010_Chat;
using System.Net;
using System.Net.Sockets;

namespace ServerChat
{
    public partial class frmServer : Form
    {
        public delegate void RefreshClientListDelegate();
        public RefreshClientListDelegate refreshClientListAction;

        private ServerChat serverChat;

        public frmServer()
        {
            InitializeComponent();
            serverChat = ServerChat.GetServer();
            refreshClientListAction = new RefreshClientListDelegate(RefreshListMethod);
            serverChat.RefreshList += new ServerChat.ReceivedRefreshClientsEventHandler(serverChat_RefreshList);
            txtIP.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork).ToString();
        }

        /*
         * Refreshes the list of clients 
         */
        private void RefreshListMethod()
        {
            ListViewItem item;
            lvClients.Items.Clear();
            btnDisconnectClient.Enabled = false;
            foreach (User user in serverChat.Users)
            {
                item = new ListViewItem(user.Name);
                item.SubItems.Add(user.IpAdress);
                item.SubItems.Add(user.Port);
                item.Tag = user;
                lvClients.Items.Add(item);
            }
            lblClients.Text = lvClients.Items.Count.ToString();
        }

        void serverChat_RefreshList(object sender, EventArgs args)
        {
            if (lvClients.InvokeRequired)
                lvClients.Invoke(refreshClientListAction);
            else
                RefreshListMethod();
        }

        private void btnArreter_Click(object sender, EventArgs e)
        {
            serverChat.Stop();
            btnDemarrer.Enabled = true;
            btnArreter.Enabled = false;
            lblEtat.Text = "Arrêté";
            txtIP.Enabled = true;
            nudPort.Enabled = true;
        }

        private void btnDemarrer_Click(object sender, EventArgs e)
        {
            serverChat.Start(txtIP.Text, int.Parse(nudPort.Value.ToString()));
            btnDemarrer.Enabled = false;
            btnArreter.Enabled = true;
            lblEtat.Text = "Démarré";
            txtIP.Enabled = false;
            nudPort.Enabled = false;
        }

        private void btnDisconnectClient_Click(object sender, EventArgs e)
        {
            serverChat.SendDisconnect(lvClients.SelectedItems[0].Tag as User);
        }

        private void lvClients_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            btnDisconnectClient.Enabled = (e.IsSelected);
        }

        private void btnFermer_Click(object sender, EventArgs e)
        {
            serverChat.Stop();
            Close();
        }
    }
}
